<?php

class SocialNetwork
{
    protected $applicationName;
    protected $apiKey;
    protected $secretKey;

    protected $callback;
    protected $oauth;
    protected $networkName = 'None';
    
    protected $apiUrl;
    protected $requestTokenPath;
    protected $authorizePath;
    protected $accessTokenPath;

    public function  __construct($applicationName, $apiKey, $secretKey, $callback =  null) {
        $this->applicationName = $applicationName;
        $this->apiKey = $apiKey;
        $this->secretKey = $secretKey;
        if(isset($callback))
            $this->callback = $callback;

        // create OAuth
        $this->oauth = new OAuth($apiKey, $secretKey);
    }

    public function authorize()
    {
        // enableDebug...
        $this->oauth->enableDebug();
        // Check if not authorized already
        if(empty($_SESSION['oauth'][$this->networkName]['authorized'])) {
            // Get the request tokens if none exist
            if(empty($_SESSION['oauth'][$this->networkName]['request']))
            {
                try
                {
                    $request = $this->oauth->getRequestToken($this->apiUrl.$this->requestTokenPath, $this->callback);

                    $_SESSION['oauth'][$this->networkName]['request'] = $request;
                    header('location:'.$this->apiUrl.$this->authorizePath.$request['oauth_token']);

                } catch(Exception $e) {
                    echo '<pre>'.$e->getMessage();echo ($request); print_r($request);
                    echo '<br>-- Debug info <br>';
                    print_r($this->oauth->debugInfo);
                    echo '<pre>Error: getRequestToken. Please try again.</pre>';
                    return false;
                }
            }
        }

        // Get the access tokens if none exist
        if(empty($_SESSION['oauth'][$this->networkName]['access'])) {
                $request = $_SESSION['oauth'][$this->networkName]['request'];
    
                $this->oauth->setToken($request['oauth_token'], $request['oauth_token_secret']);

                try {
                    $access = $this->oauth->getAccessToken($this->apiUrl.$this->accessTokenPath);
                    $_SESSION['oauth'][$this->networkName]['access'] = $access;

                } catch(Exception $e) {
                    echo '<br>-- Debug info <br>';
                    print_r($this->oauth->debugInfo);
                    echo '<pre>Error: getAccessToken. Please try again.</pre>';
                    return false;
                }
        }


        // Set the access tokens
        $access = $_SESSION['oauth'][$this->networkName]['access'];
        $this->oauth->setToken($access['oauth_token'], $access['oauth_token_secret']);
        $_SESSION['oauth'][$this->networkName]['authorized'] = true;

        return true;
    }

    /**
    * GET the response of a url.
    * $param $url the url to be fetched
    * @return xml response or false if oauth->fetch fails.
    */
    protected function get($url) {

        $this->authorize();
        $this->oauth->enableDebug();

        try {
                $this->oauth->fetch($url);
                return $this->XMLtoArray($this->oauth->getLastResponse());
        } catch(Exception $e) {
                echo '<pre>Error: GET '.$url.'</pre>';
                echo '<pre>';
                print_r($this->oauth->debugInfo);
                echo '</pre>';
                return $this->oauth->debugInfo;
        }
    }

    /**
    * Recursive function to transform xml to a workable array
    * @param $input simplexml_load_string('xml_string')
    * @return array
    */
    protected function XMLtoArray($obj) {
        $obj = is_string($obj) ? simplexml_load_string($obj) : $obj;
        $_arr = is_object($obj) ? get_object_vars($obj) : $obj;

        if(!empty($_arr)) {
                foreach ($_arr as $key => $val) {
                        $val = (is_array($val) || is_object($val)) ? $this->XMLtoArray($val) : $val;
                        $arr[$key] = $val;
                }
                return $arr;
        }
    }
    
}

?>
